﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using DemoExtNet.BusinessObject.BusinessEntities;

namespace DemoExtNet.NhanVien.UserControl
{
    public partial class UCCongViecChinh : System.Web.UI.UserControl
    {
        NSToChuc toChuc = new NSToChuc();
        NSCongViecChinh congViec = new NSCongViecChinh();
        private List<NSCongViecChinh> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            txtCongViec.Text = string.Empty;

            dtNgayThayDoi.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtGhiChu.Text = string.Empty;
            hdCoQuan_CongViecChinh.Value = string.Empty;
            txtChucVu.Text = string.Empty;
            txtCoQuan.Text = string.Empty;
            hdFlagToChuc_CongViecChinh.Value = string.Empty;
        }

        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
            hdNhanVienId.Text = nhanvienID;
            hdCongViecChinhId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdCongViecChinh.Title = "Tạo mới công việc chính";
                ResetControl();
                hdType.Text = type;
                btnSave.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdCongViecChinh.Title = "Sửa đổi công việc chính";
                hdType.Text = type;
                btnSave.Hidden = false;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdCongViecChinh.Title = "Xem chi tiết công việc chính";
                hdType.Text = type;
                btnSave.Hidden = true;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            wdCongViecChinh.Show();
        }
        public void LoadDataNBL(int id)
        {
            lst = (List<NSCongViecChinh>)Session["CongViecChinh"];
            NSCongViecChinh item = lst.Where(a => a.CongViecChinhId.Equals(id)).SingleOrDefault();
            dtNgayThayDoi.Text = item.ThoiGianThayDoi.ToString("dd/MM/yyyy");
            txtGhiChu.Text = item.GhiChu;
            txtCongViec.Text = item.Ten;
            txtChucVu.Text = item.ChucVu;
            hdFlagToChuc_CongViecChinh.Value = item.ToChucFlag_CongViecChinh.HasValue ? item.ToChucFlag_CongViecChinh.Value.ToString() : string.Empty;
            if (item.ToChucFlag_CongViecChinh.HasValue && item.ToChucFlag_CongViecChinh.Value)
            {
                txtCoQuan.Text = item.CoQuanId > 0 ? toChuc.LayTenToChuc(item.CoQuanId.Value) : string.Empty;
                hdCoQuan_CongViecChinh.Value = item.CoQuanId.Value.ToString();
            }
            else
            {
                txtCoQuan.Text = item.CoQuanKhac;
            }

        }
        protected void btnSaveCongViecChinh_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayHieuLuc = Convert.ToDateTime(dtNgayThayDoi.Text);
            int coQuanID = 0;
            bool tochucFlag = Convert.ToBoolean(hdFlagToChuc_CongViecChinh.Value);
            if (Convert.ToBoolean(hdFlagToChuc_CongViecChinh.Value))
            {
                coQuanID = !string.IsNullOrEmpty(hdCoQuan_CongViecChinh.Text) ? Convert.ToInt32(hdCoQuan_CongViecChinh.Text) : 0;
            }
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Value.ToString()) ? Convert.ToInt16(hdNhanVienId.Value) : 0;
            if (hdType.Text.Equals("AddNew"))
            {
                NSCongViecChinh item = congViec.GetCongViecChinh(nhanVienId, txtCongViec.Text, ngayHieuLuc, txtGhiChu.Text, txtChucVu.Text, coQuanID, tochucFlag, txtCoQuan.Text);
                ((UCNhanVien)this.Parent).GetCongViecChinh(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                congViec.EditCongViecChinh(Convert.ToInt16(hdCongViecChinhId.Text), txtCongViec.Text, ngayHieuLuc, txtGhiChu.Text, txtChucVu.Text, coQuanID, tochucFlag, txtCoQuan.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("CongViecChinh");
            }

            wdCongViecChinh.Hide();

        }
        [DirectMethod]
        public void LoadLookUpToChuc()
        {
            bool flag=!string.IsNullOrEmpty(hdFlagToChuc_CongViecChinh.Value.ToString())?Convert.ToBoolean(hdFlagToChuc_CongViecChinh.Value.ToString()):true;
            UcToChuc.ShowLookUp_ToChuc("ToChucKhac",flag, txtCoQuan.Text);
        }

    }
}